import axios from 'axios'
import config from './config'
import Vue from 'vue'
import * as errorCode from './error'
import utils from '../utils'
// Request 拦截器
axios.interceptors.request.use(
  reConfig => {
    // Vue.$vux.loading.show()
    return reConfig
  },
  error => {
    return Promise.reject(error)
  }
)
// Response 拦截器
axios.interceptors.response.use(
  response => {
    Vue.$vux.toast.hide()
    if (response) {
      var res = response.data
      switch (res.status.toString()) {
        case '200':
          Vue.$vux.loading.hide()
          return res
        case 200:
          Vue.$vux.loading.hide()
          return res
        case '400001001': // 用户不存在，需要注册 TODO: 需要添加错误码区分
          return res
        case '40001':
          localStorage.setItem('status', '-5')
          utils.redirectWx('-5') // Token过期，重新授权登录
          break
        default:
          Vue.$vux.toast.show({
            text: errorCode['CODE_' + res.status],
            width: '10em'
          })
          return res
      }
    } else {
      Vue.$vux.toast.show({
        type: 'warn',
        text: '网络出错，请稍候再试'
      })
    }
  },
  error => {
    return error
  }
)
var timeStamp = String(parseInt(Number(new Date()) / 1000))
var tokenval = utils.AnonToken(timeStamp) // 用户登录前
export default {
  // 签名
  jssdk (param) {
    return axios.post('/weixin/jssdk/getsignature', param, config)
  },
  // 获取图片验证码
  captcha (captchaId) {
    return axios.get('/captcha/captcha?captchaId=' + captchaId, config)
  },
  // 图片验证码验证
  validcode (param) {
    return axios.get('/captcha/valid?captchaId=' + param.captchaId + '&captcha=' + param.captcha, config)
  },
  // 广告位，1信贷客 2信贷圈 3个人中心
  ads (param) {
    return axios.get('/advertisement/get?token=' + tokenval + '&timestamp=' + timeStamp + '&userId=' + param.userid + '&position=' + param.addtype, config)
  },
  // 发送短信验证码
  sendCode (param) {
    return axios.post('/vcode/send', param, config)
  },
  // 短信验证码校验
  validate (param) {
    return axios.post('/vcode/validate', param, config)
  },
  // 验证手机是否已经注册
  check (param) {
    return axios.post('/user/mobile/check', param, config)
  },
  // 注册账号
  register (param) {
    return axios.post('/user/register', param, config)
  },
  // 用户登录
  login (param) {
    return axios.post('/user/login', param, config)
  },
  logout (param) {
    return axios.post('/user/logout', param, config)
  },
  // 验证客户手机登录
  customer (param) {
    return axios.post('/customer/vcode', param, config)
  },
  // 查询客户信息
  getLatest (param) {
    return axios.post('/customer/apply/getLatest', param, config)
  },
  // 新建借款申请
  addcustomer (param) {
    return axios.post('/customer/apply/add', param, config)
  },
  // 忘记密码
  forget (param) {
    return axios.post('/user/password/forget', param, config)
  },
  // 图片上传 (图文推广)
  upload (param) {
    return axios.post('image/upload/weixin', param, config)
  },
  // 信贷客
  creditloan (param) {
    return axios.get('/partner/list?token=' + tokenval + '&timestamp=' + timeStamp + '&page=' + param.page + '&perPage=' + param.perPage + '&partnerType=' + param.partnerType, config)
  },
  // 信贷客详情页
  creditloanDetail (param) {
    return axios.get('/partner/info?token=' + tokenval + '&timestamp=' + timeStamp + '&userId=' + param.userId + '&partnerId=' + param.partnerId, config)
  },
  // 内容推广
  contentpromote (param) {
    return axios.get('/post/list?token=' + tokenval + '&timestamp=' + timeStamp + '&type=' + param.typenum + '&keyword=' + param.keyword + '&page=' + param.page + '&perPage=' + param.perPage, config)
  },
  // 内容推广-文章分享
  articleshare (param) {
    return axios.post('/post/share', param, config)
  },
  // 内容推广-内容详情页
  articledetail (param) {
    return axios.get('/post/detail?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid + '&postId=' + param.id, config)
  },
  // 点击收藏
  collect (param) {
    return axios.post('/post/collect', param, config)
  },
    // 图片推广
  template (param) {
    return axios.get('/image/template/list?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid + '&page=' + param.page + '&perPage=' + param.perPage + '&partnerId=' + param.partnerId, config)
  },
  // 已使用图片
  userimg (param) {
    return axios.get('/image/mine?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid + '&page=' + param.page + '&perPage=' + param.perPage, config)
  },
  // 我的收藏
  // 文章收藏
  articlecollect (param) {
    return axios.get('post/mycollect?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid + '&page=' + param.page + '&perPage=' + param.perPage, config)
  },
  // 添加宣传语
  tagline (param) {
    return axios.get('/image/detail?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&id=' + param.picId + '&userId=' + param.userid, config)
  },
  // 图文推广保存
  save (param) {
    return axios.post('/image/save', param, config)
  },
  // 跳骚市场
  // 交易大厅
  deal (param) {
    return axios.get('/fly/order/list?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid + '&amount=' + param.amount + '&term=' + param.term + '&transferCount=' + param.transferCount + '&pushTime=' + param.pushTime + '&page=' + param.pagenum + '&perPage=' + param.perpagenum, config)
  },
  // 已认领客户
  claim (param) {
    return axios.get('/fly/order/mine?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid + '&page=' + param.page + '&perPage=' + param.perPage, config)
  },
  // 已认领客户-获取某个客户的联系方式
  relocation (param) {
    return axios.post('/fly/order/contact ', param, config)
  },
  // 已认领客户-获取飞单费用信息
  feemessage (param) {
    return axios.post('/fly/order/fee', param, config)
  },
  // 已认领客户-将某个客户转让给平台
  ordercession (param) {
    return axios.post('/fly/order/cession', param, config)
  },
  // 我要认领
  claimclient (param) {
    return axios.post('/fly/order/claim', param, config)
  },
  // 个人中心 // 我要提现
  person (param) {
    return axios.get('/account/info?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid, config)
  },
  persons (paramUserid, paramToken) {
    return axios.get('/account/info?token=' + paramToken + '&timestamp=' + timeStamp + '&userId=' + paramUserid, config)
  },
  // 收支明细记录
  BalancePayment (param) {
    return axios.post('/account/transaction/list', param, config)
  },
  // 结算账户记录
  balance (param) {
    return axios.post('/account/settle/list', param, config)
  },
  // 个人信息
  message (param) {
    return axios.get('/user/info?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid, config)
  },
  // 修改用户姓名
  changename (param) {
    return axios.post('/user/info/name/update?', param, config)
  },
  // 修改手机号
  changemobile (param) {
    return axios.post('/user/mobile/update', param, config)
  },
  // 修改提现卡
  // 银行列表
  bank () {
    return axios.get('/pay/banks?token=' + tokenval + '&timestamp=' + timeStamp, config)
  },
  // 绑卡
  bind (param) {
    return axios.post('/pay/card/bind', param, config)
  },
  // 我要提现
  // 提现记录
  applymylist (param) {
    return axios.post('/account/cash/apply/mylist', param, config)
  },
  // 提现账号维护
  accountmessage (param) {
    return axios.get('/pay/card/info?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid, config)
  },
  // 提现申请
  depositapply (param) {
    return axios.post('/account/cash/apply', param, config)
  },
  // 客户列表
  client (param) {
    return axios.get('/customer/apply/mine?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid + '&page=' + param.page + '&perPage=' + param.perPage + '&partnerId=' + param.partnerId + '&commonStatus=' + param.commonStatus + '&amount=' + param.amount + '&applyCnt=' + param.applyCnt, config)
  },
  // 客户列表-将某个客户转让给平台
  cession (param) {
    return axios.post('/fly/order/apply/cession', param, config)
  },
  // 交易记录
  earnings (param) {
    return axios.get('/account/income/list?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid + '&partnerId=' + param.partnerId + '&searchDate=' + param.searchDate, config)
  },
  // 公告
  notice () {
    return axios.get('/notice/list?token=' + tokenval + '&' + 'timestamp=' + timeStamp, config)
  },
  // 公告明细
  noticedetail (param) {
    return axios.get('/notice/get?token=' + tokenval + '&' + 'timestamp=' + timeStamp + '&id=' + param.id, config)
  },
  // 咨询 咨询问题列表
  consult (param) {
    return axios.get('/qa/list?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid, config)
  },
  // 提交新问题
  newquestion (param) {
    return axios.post('/qa/question/add', param, config)
  },
  // 信贷圈子-内容列表
  creditcircle (param) {
    return axios.get('/topic/list?token=' + tokenval + '&timestamp=' + timeStamp + '&userId=' + param.userid + '&page=' + param.page + '&perPage=' + param.perPage, config)
  },
  // 信贷圈子-获取内容评论列表
  contentreview () {
    return axios.get('/topic/comment/list?token=' + tokenval + '&' + 'timestamp=' + timeStamp, config)
  },
  // 信贷圈子-发布新内容
  issue (param) {
    return axios.post('/topic/add', param, config)
  },
  // 信贷圈子-内容回复
  topictreply (param) {
    return axios.post('/topic/comment/add', param, config)
  },
  // 信贷圈子-获取内容评论列表
  comment (param) {
    return axios.get('/topic/comment/list?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid + '&topicId=' + param.id + '&page=' + param.page + '&perPage=' + param.perPage + '&sort=' + param.sort, config)
  },
  // 信贷圈子-评论点赞
  commentlike (param) {
    return axios.get('/topic/comment/like?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid + '&id=' + param.likeId, config)
  },
  // 信贷圈子关注内容
  attentioncontent (param) {
    return axios.get('/topic/watch?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid + '&id=' + param.contentId, config)
  },
  // 信贷圈子-获取内容明细 (回复需要调用)
  contentdetail (param) {
    return axios.get('/topic/get?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid + '&id=' + param.id, config)
  },
  // 邀请好友
  // 我的邀请码 团队收益
  bymonth (param) {
    return axios.get('/account/income/team/byMonth?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid, config)
  },
  // 今日邀请用户
  today (param) {
    return axios.get('/user/invite/day?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid, config)
  },
  // 我的团队收益明细
  teamdetail (param) {
    return axios.get('/account/income/team/detail?token=' + param.Tokenuser + '&timestamp=' + param.timestamp + '&userId=' + param.userid + '&page=' + param.page + '&perPage=' + param.perPage, config)
  },
  // 工具箱
  toolbox () {
    return axios.get('/toolbox/list?token=' + tokenval + '&' + 'timestamp=' + timeStamp, config)
  }
}
